const mongoose = require("mongoose");
class Db {
	constructor(model) {
		this.model = model;
	}
	_connect(){
		// 如果已经连接上了,返回一个成功的Promise实例
		if(mongoose.connection.readyState === 1) return Promise.resolve();
		return mongoose.connect("mongodb://127.0.0.1:27017/dahaida",{
			serverSelectionTimeoutMs:1000
		})
	}
	// 插入一条文档
	async insertOne(obj){
		await this._connect();// 连接数据库
		return this.model(obj).save();
	}
}
// 生成Schema
const schema = new mongoose.Schema({
	// 微博内容
	context:{
		type:String,
		required:true,
		unique:true
	},
	// 顶
	topNum:{
		type:Number,
		default:0
	},
	// 踩
	downNum:{
		type:Number,
		default:0
	},
	// 添加时间
	addTime:{
		type:String,
		required:true
	}
},{
	versionKey:false
})
const model = mongoose.model("contextList",schema,"contextList");
const db = new Db(model);
db.insertOne({
	context:"abcde",
	addTime:"abcd"
}).then(value=>{
	console.log(value)
})